package com.vast.admin.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vast.common.core.domain.entity.SysUser;
import com.vast.admin.entity.PlaceholderTemplate;
import com.vast.admin.mapper.PlaceholderTemplateMapper;
import com.vast.system.exception.BusinessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class PlaceholderTemplateService extends ServiceImpl<PlaceholderTemplateMapper, PlaceholderTemplate> {

    /**
     * 保存或更新签署者模板
     * @param placeholderTemplate
     * @param manager
     */
    @Transactional
    public void savePlaceholderBelongTemplate(PlaceholderTemplate placeholderTemplate, SysUser manager) {
        //校验是否重复
        QueryWrapper<PlaceholderTemplate> placeholderTemplateQueryWrapper = new QueryWrapper<>();
        placeholderTemplateQueryWrapper.eq(PlaceholderTemplate.COL_PLACEHOLDER_BELONG, placeholderTemplate.getPlaceholderBelong())
                .eq(PlaceholderTemplate.COL_TYPE, 2)
                .ne(placeholderTemplate.getId() != null, PlaceholderTemplate.COL_ID, placeholderTemplate.getId());
        if (super.count(placeholderTemplateQueryWrapper) > 0) {
            throw new BusinessException("该名称已存在");
        }
        placeholderTemplate.setType(2);
        if (placeholderTemplate.getId() == null) {
            placeholderTemplate.setCreateTime(new Date());
            placeholderTemplate.setCreateBy(manager.getUserId());
        } else {
            placeholderTemplate.setUpdateTime(new Date());
            placeholderTemplate.setUpdateBy(manager.getUserId());
        }
        //保存归属模板
        super.saveOrUpdate(placeholderTemplate);
        //保存占位符模板
        if (placeholderTemplate.getPlaceholderTemplates() != null && !placeholderTemplate.getPlaceholderTemplates().isEmpty()) {
            savePlaceholderNameTemplates(placeholderTemplate.getPlaceholderBelong(), placeholderTemplate.getPlaceholderTemplates());
        }
    }

    /**
     * 保存占位符模板
     * @param placeholderTemplates
     */
    private void savePlaceholderNameTemplates(String belong, List<PlaceholderTemplate> placeholderTemplates) {
        QueryWrapper<PlaceholderTemplate> placeholderTemplateQueryWrapper = new QueryWrapper<>();
        placeholderTemplateQueryWrapper.eq(PlaceholderTemplate.COL_PLACEHOLDER_BELONG, belong)
                .eq(PlaceholderTemplate.COL_TYPE, 1);
        super.remove(placeholderTemplateQueryWrapper);
        placeholderTemplates.forEach(item -> item.setPlaceholderBelong(belong));
        super.saveBatch(placeholderTemplates);
    }

    /**
     * 删除
     * @param id
     */
    public void removePlaceholderTemplate(Long id) {
        PlaceholderTemplate placeholderTemplate = super.getById(id);
        if (placeholderTemplate == null) {
            throw new BusinessException("该属性模板已删除");
        }
        QueryWrapper<PlaceholderTemplate> placeholderTemplateQueryWrapper = new QueryWrapper<>();
        placeholderTemplateQueryWrapper.eq(PlaceholderTemplate.COL_PLACEHOLDER_BELONG, placeholderTemplate.getPlaceholderBelong());
        super.remove(placeholderTemplateQueryWrapper);
    }

    /**
     * 获取占位符模板
     * @param belong
     * @param name
     * @return
     */
    public PlaceholderTemplate getPlaceholderNameTemplateByBelongAndName(String belong, String name) {
        QueryWrapper<PlaceholderTemplate> placeholderTemplateQueryWrapper = new QueryWrapper<>();
        placeholderTemplateQueryWrapper.eq(PlaceholderTemplate.COL_PLACEHOLDER_BELONG, belong)
                .eq(PlaceholderTemplate.COL_PLACEHOLDER_NAME, name)
                .eq(PlaceholderTemplate.COL_TYPE, 1);
        List<PlaceholderTemplate> placeholderTemplates = super.list(placeholderTemplateQueryWrapper);
        if (placeholderTemplates == null || placeholderTemplates.isEmpty()) {
            return null;
        }
        return placeholderTemplates.get(0);
    }

    /**
     * 获取签署方模板
     * @param belong
     * @return
     */
    public PlaceholderTemplate getPlaceholderBelongTemplateByBelong(String belong) {
        QueryWrapper<PlaceholderTemplate> placeholderTemplateQueryWrapper = new QueryWrapper<>();
        placeholderTemplateQueryWrapper.eq(PlaceholderTemplate.COL_PLACEHOLDER_BELONG, belong)
                .eq(PlaceholderTemplate.COL_TYPE, 2);
        List<PlaceholderTemplate> placeholderTemplates = super.list(placeholderTemplateQueryWrapper);
        if (placeholderTemplates == null || placeholderTemplates.isEmpty()) {
            return null;
        }
        return placeholderTemplates.get(0);
    }

    /**
     * 分页查询
     * @return
     */
    public IPage<PlaceholderTemplate> list(PlaceholderTemplate placeholderTemplate, Page<PlaceholderTemplate> placeholderTemplatePage) {
        QueryWrapper<PlaceholderTemplate> placeholderTemplateQueryWrapper = new QueryWrapper<>();
        placeholderTemplateQueryWrapper.eq(PlaceholderTemplate.COL_TYPE, 2);
        return super.page(placeholderTemplatePage, placeholderTemplateQueryWrapper);
    }

    /**
     * id获取
     * @param id
     * @return
     */
    public PlaceholderTemplate getBelongPlaceholderTemplateById(Long id) {
        PlaceholderTemplate placeholderTemplate = super.getById(id);
        placeholderTemplate.setPlaceholderTemplates(getPlaceholderTemplatesByBelong(placeholderTemplate.getPlaceholderBelong()));
        return placeholderTemplate;
    }

    /**
     * 获取归属下的所有占位符模板
     * @param belong
     * @return
     */
    private List<PlaceholderTemplate> getPlaceholderTemplatesByBelong(String belong) {
        QueryWrapper<PlaceholderTemplate> placeholderTemplateQueryWrapper = new QueryWrapper<>();
        placeholderTemplateQueryWrapper.eq(PlaceholderTemplate.COL_PLACEHOLDER_BELONG, belong)
                .eq(PlaceholderTemplate.COL_TYPE, 1);
        return super.list(placeholderTemplateQueryWrapper);
    }

}
